package club.framework.web.application.sys.menu.controller;

import club.framework.web.application.sys.menu.entity.Menu;
import club.framework.web.application.sys.menu.service.IMenuService;
import club.framework.web.base.entity.BasePageReqEntity;
import club.framework.web.base.entity.Result;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by 念梓  on 2017/9/5.
 * Email:sunmch@163.com
 * author: 念梓
 * des:
 */
@Controller
@RequestMapping("sys/menu")
public class MenuController {

    private static final Logger log = LogManager.getLogger(MenuController.class);

    @Resource
    private IMenuService menuService;

    @RequestMapping("index")
    public String index() {
        return "application/sys/menu/index";
    }

    @RequestMapping("add")
    @ResponseBody
    public Object add(Menu menu) {
        Result result = new Result();
        try {
            menuService.save(menu);
        } catch (Exception ex) {
            log.error("菜单： {}，新增失败，失败信息: {}", menu, ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public Object update(Menu menu) {
        Result result = new Result();
        try {
            menuService.update(menu);
        } catch (Exception ex) {
            result.setMsg(ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            log.error("菜单信息: {},更新失败: {}", menu, ex.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(int id) {
        return null;
    }

    @RequestMapping("view")
    public String view(int id) {
        return null;
    }

    @RequestMapping("loadpage")
    @ResponseBody
    public Object loadPage(@RequestBody BasePageReqEntity<Menu> basePageReqEntity) {
        return menuService.queryPage(basePageReqEntity);
    }

    @RequestMapping("loadmenubystep")
    @ResponseBody
    public Object loadMenuByStep(@RequestParam("step") int step) {
        Result result = new Result();
        try {
            List<Menu> menus = menuService.loadMenuByStep(step);
            result.setData(menus);
        } catch (Exception ex) {
            log.error("根据级别: {},获取父菜单失败,失败信息: {}", step, ex.getMessage());
            result.setCode(Result.ERROR_CODE);
        }
        return result;
    }

    /**
     * 加载所有的菜单级别
     *
     * @return
     */
    @RequestMapping("loadmenustep")
    @ResponseBody
    public Object loadMenuStep() {
        Result result = new Result();
        try {
            List<Map<String, String>> stepMenuList = menuService.loadMenuStep();
            result.setData(stepMenuList);
        } catch (Exception ex) {
            log.error("拉取目前菜单有几个层级失败,失败信息: {}", ex.getMessage());
            result.setCode(Result.ERROR_CODE);
        }
        return result;
    }

    @RequestMapping("loadmenubyparentid")
    @ResponseBody
    public Object loadMenuByParentId(@RequestParam("parentId") int parentId) {
        Result result = new Result();
        try {
            List<Menu> menuList = menuService.loadMenuByParentId(parentId);
            result.setData(menuList);
        } catch (Exception ex) {
            log.error("根据父菜单id: {},获取第一级子菜单失败: {}", parentId, ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("deletemenubycode")
    @ResponseBody
    public Object deleteMenuByCode(String code) {
        Result result = new Result();
        try {
            menuService.deleteMenuByCode(code);
        } catch (Exception ex) {
            log.error("根据code: {},删除信息失败: {}", code, ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("getmenubyid")
    @ResponseBody
    public Object getMenuById(int id) {
        Result result = new Result();
        try {
            result.setData(menuService.getObject(id));
        } catch (Exception ex) {
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("getmenutreebyparentid")
    @ResponseBody
    public Object getMenuTreeByParentId(@RequestParam("parentid") int parentId,@RequestParam("userid") int userId){
        return menuService.loadMenuTreeByParentId(parentId,userId);
    }







}
